/*************************************************************************
	> File Name: JuzList.h
	> Author: 
	> Mail: 
	> Created Time: 2017年06月14日 星期三 12时52分07秒
 ************************************************************************/

#ifndef _JUZLIST_H
#define _JUZLIST_H

#include <pthread.h>
#include<list>
#include<iostream>
using namespace std;

template<class T>
class JuzList :public std::list<T>
{
public:
	~JuzList();

	bool readLock();
	bool writeLock();
	bool tryReadLock();
	bool tryWriteLock();
	bool unLock();

	pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
};
template<class T>
bool JuzList<T>::readLock()
{
	static int nResult = -1;
	nResult = pthread_rwlock_rdlock(&rwlock);
	return nResult == 0;
}

template<class T>
bool JuzList<T>::writeLock()
{
	static int nResult = -1;
	nResult = pthread_rwlock_wrlock(&rwlock);
	return nResult == 0;
}

template<class T>
bool JuzList<T>::tryReadLock()
{
	static int nResult = -1;
	nResult = pthread_rwlock_tryrdlock(&rwlock);
	return nResult == 0;
}

template<class T>
bool JuzList<T>::tryWriteLock()
{
	static int nResult = -1;
	nResult = pthread_rwlock_trywrlock(&rwlock);
	return nResult == 0;
}

template<class T>
bool JuzList<T>::unLock()
{
	static int nResult = -1;
	nResult = pthread_rwlock_unlock(&rwlock);
	if(nResult != 0)
	{
		cout<<"unLock 失败!!!请查找原因!"<<endl;
	}
	return nResult == 0;
}
template<class T>
JuzList<T>::~JuzList()
{
	pthread_rwlock_destroy(&rwlock);
}

#endif
